import Yoga from "yoga-layout";
import {
  displayMap,
  flexDirectionMap,
  justifyMap,
  alignMap,
  wrapMap,
  positionMap
} from "@/constants/index.ts";
import type { Node } from "yoga-layout";
const defaultLayout = {
  width: 50,
  height: 50,
  display: "flex",
  flexDirection: "row",
  justifyContent: "space-between",
  alignContent: "center",
  // alignItems: "stretch",
  flexWrap: "nowrap",
  // flex: "0 1 auto",
  // flexShrink: 0,
  // flexGrow: 0,
  // flexBasis: "auto",
  // aspectRatio: undefined,
  position: "relative",
  // gap: 0,
  // layoutDirection: "ltr",
  // minWidth: undefined,
  // minHeight: undefined,
  // maxWidth: undefined,
  // maxHeight: undefined,
  // alignSelf: "center",
  top: 0,
  left: 0,
  right: 0,
  bottom: 0
  // margin: 0,
  // padding: 0,
  // border: 0
} as const;

export function capitalizeKey(k: string) {
  switch (k) {
    case "position":
      return "setPositionType";
  }
  return "set" + k[0].toUpperCase() + k.slice(1);
}

export function capitalizeValue(v: string, k: string) {
  switch (k) {
    case "display":
      return displayMap[v as keyof typeof displayMap];
    case "flexDirection":
      return flexDirectionMap[v as keyof typeof flexDirectionMap];
    case "justifyContent":
      return justifyMap[v];
    case "alignContent" || "alignItems":
      return alignMap[v];
    case "flexWrap":
      return wrapMap[v];
    case "position":
      return positionMap[v];
    default:
      return v;
  }
}
// let Yoga: any;
// // 使用同步方式初始化 Yoga
// async function initYoga() {
//   if (!Yoga) {
//     Yoga = await loadYoga();
//   }
//   return Yoga;
// }

export function setYogaStyle(style = {}, node: Node) {
  Object.keys(style).forEach((key) => {
    const value = capitalizeValue(style[key as keyof typeof style], key);
    if (value !== undefined) {
      const setterName = capitalizeKey(key) as keyof Node;
      if (typeof node[setterName] === "function") {
        (node[setterName] as Function)(value);
      }
    }
  });
}

export function createNode(Yoga, style = {}) {
  let node = Yoga.Node.create();
  const layout = { ...defaultLayout, ...style };
  setYogaStyle(layout, node);
  return { node, style };
}

export function generateLayoutTree(node: any, parentOffset = { x: 0, y: 0 }) {
  const layout = node.getComputedLayout();

  // 计算当前节点的绝对位置（累加父节点的偏移量）
  const absolutePosition = {
    left: parentOffset.x + layout.left,
    top: parentOffset.y + layout.top,
    width: layout.width,
    height: layout.height
  };

  const nodeData = {
    layout: absolutePosition,
    children: []
  };

  // 递归处理子节点，传递累积的偏移量
  const childCount = node.getChildCount();
  if (childCount > 0) {
    for (let i = 0; i < childCount; i++) {
      const childNode = node.getChild(i);
      nodeData.children.push(
        generateLayoutTree(childNode, {
          x: absolutePosition.left,
          y: absolutePosition.top
        })
      );
    }
  }
  return nodeData;
}